using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Xml;

namespace OutlookAddIn_Collaboratori
{
    [ComVisible(true)]
	public class SattingsPage : System.Windows.Forms.UserControl , Outlook.PropertyPage 
	{

        private bool isDirty;
		private Outlook.PropertyPageSite ppSite;
        private PictureBox pictureBox1;
        private MaskedTextBox tPassword;
        private TextBox tDomen;
        private Label label3;
        private TextBox tName;
        private Label label2;
        private Label label1;

		private System.ComponentModel.Container components = null;
        private Button Loading;
        private OpenFileDialog openFileDialog;


        private AddinSattings m_satting;
        private TextBox textBox1;
        private Panel panel1;
        private Panel panel2;
        private Label label4;
        private CheckBox showPassword;
        private string m_path;

        public SattingsPage(AddinSattings satting,string path)
		{
            m_path = path;
            m_satting = satting;
			InitializeComponent();
			isDirty = false;
		}


		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SattingsPage));
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.tPassword = new System.Windows.Forms.MaskedTextBox();
            this.tDomen = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.tName = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.Loading = new System.Windows.Forms.Button();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.showPassword = new System.Windows.Forms.CheckBox();
            this.panel2 = new System.Windows.Forms.Panel();
            this.label4 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.ErrorImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.ErrorImage")));
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.InitialImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.InitialImage")));
            this.pictureBox1.Location = new System.Drawing.Point(194, 27);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(189, 68);
            this.pictureBox1.TabIndex = 7;
            this.pictureBox1.TabStop = false;
            // 
            // tPassword
            // 
            this.tPassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            this.tPassword.ForeColor = System.Drawing.Color.Black;
            this.tPassword.Location = new System.Drawing.Point(88, 43);
            this.tPassword.Name = "tPassword";
            this.tPassword.PasswordChar = '*';
            this.tPassword.Size = new System.Drawing.Size(231, 20);
            this.tPassword.TabIndex = 2;
            this.tPassword.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.tPassword_MaskInputRejected);
            // 
            // tDomen
            // 
            this.tDomen.Location = new System.Drawing.Point(88, 69);
            this.tDomen.Name = "tDomen";
            this.tDomen.Size = new System.Drawing.Size(231, 20);
            this.tDomen.TabIndex = 3;
            this.tDomen.TextChanged += new System.EventHandler(this.tDomen_TextChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(13, 72);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 13);
            this.label3.TabIndex = 12;
            this.label3.Text = "Domen";
            // 
            // tName
            // 
            this.tName.Location = new System.Drawing.Point(88, 17);
            this.tName.Name = "tName";
            this.tName.Size = new System.Drawing.Size(231, 20);
            this.tName.TabIndex = 1;
            this.tName.TextChanged += new System.EventHandler(this.tName_TextChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(13, 46);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 13);
            this.label2.TabIndex = 10;
            this.label2.Text = "Password";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 20);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 9;
            this.label1.Text = "Name";
            // 
            // Loading
            // 
            this.Loading.Location = new System.Drawing.Point(167, 50);
            this.Loading.Name = "Loading";
            this.Loading.Size = new System.Drawing.Size(136, 23);
            this.Loading.TabIndex = 13;
            this.Loading.Text = "Loading file configuration";
            this.Loading.UseVisualStyleBackColor = true;
            this.Loading.Click += new System.EventHandler(this.Loading_Click);
            // 
            // openFileDialog
            // 
            this.openFileDialog.DefaultExt = "*.xml";
            this.openFileDialog.Filter = "(*.xml)|*.xml|(*.txt)|*.txt|All files (*.*)|*.*\"";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(88, 24);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(313, 20);
            this.textBox1.TabIndex = 14;
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.showPassword);
            this.panel1.Controls.Add(this.tName);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.tPassword);
            this.panel1.Controls.Add(this.label3);
            this.panel1.Controls.Add(this.tDomen);
            this.panel1.Location = new System.Drawing.Point(123, 101);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(335, 125);
            this.panel1.TabIndex = 15;
            // 
            // showPassword
            // 
            this.showPassword.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
            this.showPassword.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.showPassword.AutoSize = true;
            this.showPassword.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.showPassword.Location = new System.Drawing.Point(221, 95);
            this.showPassword.Name = "showPassword";
            this.showPassword.Size = new System.Drawing.Size(98, 17);
            this.showPassword.TabIndex = 13;
            this.showPassword.Text = "Show password";
            this.showPassword.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.showPassword.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.showPassword.UseVisualStyleBackColor = true;
            this.showPassword.CheckedChanged += new System.EventHandler(this.showPassword_CheckedChanged);
            // 
            // panel2
            // 
            this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel2.Controls.Add(this.label4);
            this.panel2.Controls.Add(this.textBox1);
            this.panel2.Controls.Add(this.Loading);
            this.panel2.Location = new System.Drawing.Point(76, 232);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(425, 86);
            this.panel2.TabIndex = 16;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(13, 27);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(62, 13);
            this.label4.TabIndex = 15;
            this.label4.Text = "Path Config";
            // 
            // SattingsPage
            // 
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.pictureBox1);
            this.Name = "SattingsPage";
            this.Size = new System.Drawing.Size(670, 403);
            this.Load += new System.EventHandler(this.MyOptionPage_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		#region PropertyPage Members

		public bool Dirty
		{
			get
			{
				return isDirty;
			}
		}

		public void GetPageInfo(ref string HelpFile, ref int HelpContext)
		{
			// TODO:  Add MyOptionPage.GetPageInfo implementation
		}

		public void Apply()
		{
            m_satting.UserName = tName.Text;
            m_satting.Password = tPassword.Text;
            m_satting.Domian = tDomen.Text;
            if (doc != null)
            {
                doc.Save(m_path);
                System.Windows.Forms.MessageBox.Show("Configuration changes will take effect after you restart Outlook");
            }
		}

		#endregion


		private void MyOptionPage_Load(object sender, System.EventArgs e)
		{
			Type myType = typeof(System.Object); 
			string assembly = 
			System.Text.RegularExpressions.Regex.Replace(myType.Assembly.CodeBase, "mscorlib.dll", "System.Windows.Forms.dll");
			assembly = System.Text.RegularExpressions.Regex.Replace(assembly, "file:///", "");
			assembly = System.Reflection.AssemblyName.GetAssemblyName(assembly).FullName;
			Type unmanaged =
				Type.GetType(System.Reflection.Assembly.CreateQualifiedName
				(assembly, "System.Windows.Forms.UnsafeNativeMethods"));
			Type oleObj = unmanaged.GetNestedType("IOleObject");
			System.Reflection.MethodInfo mi = oleObj.GetMethod("GetClientSite");
			object myppSite = mi.Invoke(this, null);
			this.ppSite = (Outlook.PropertyPageSite)myppSite;
            ////
            tName.Text = m_satting.UserName;
            tPassword.Text = m_satting.Password;
            tDomen.Text = m_satting.Domian;
            textBox1.Text = m_path;
            IsInicializ = true;


		}

		[DispId(-518)]
		public string Caption
		{
            get { return "Investa Finance"; }
		}

        bool IsInicializ = false;
        private void tName_TextChanged(object sender, EventArgs e)
        {
            if (IsInicializ)
            {
                isDirty = true;
                ppSite.OnStatusChange();
            }
        }

        private void tPassword_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            if (IsInicializ)
            {
                isDirty = true;
                ppSite.OnStatusChange();
            }
        }

        private void tDomen_TextChanged(object sender, EventArgs e)
        {
            if (IsInicializ)
            {
                isDirty = true;
                ppSite.OnStatusChange();
            }
        }

        XmlDocument doc = null;
        private void Loading_Click(object sender, EventArgs e)
        {
            if (IsInicializ)
            {
                DialogResult rez = openFileDialog.ShowDialog();
                if (rez == DialogResult.OK)
                {
                    try
                    {
                        doc = new XmlDocument();
                        doc.Load(openFileDialog.FileName);
                        isDirty = true;
                        ppSite.OnStatusChange();
                    }
                    catch
                    {
                        System.Windows.Forms.MessageBox.Show("Bad format XML");
                    }
                }
            }
        }

        private void showPassword_CheckedChanged(object sender, EventArgs e)
        {
            char c = new char();// '3';
            if (!showPassword.Checked)
                c = '*';

            tPassword.PasswordChar = c;
        }
	}
}
